package com.ruoyi.mall.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.mall.api.domian.RecommendProductsVO;
import com.ruoyi.mall.service.RecommendProductsStrategy;
import com.ruoyi.system.api.domain.UploadPicture;
import com.ruoyi.system.api.RemoteUploadService;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.api.domian.Product;
import com.ruoyi.mall.service.IProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品Controller
 * 
 * @author luogongquan
 * @date 2024-09-25
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController
{
    @Autowired
    private IProductService productService;
    @Autowired
    private RemoteUploadService remoteUploadService;
    @Autowired
    private RecommendProductsStrategy recommendProductsStrategy;
    /**
     * 查询商品列表
     */
    @RequiresPermissions("mall:product:list")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }
    /**
     * 查询商品列表
     */
    @GetMapping("wx/list")
    public TableDataInfo wxList(Product product)
    {
        startPage();
        product.setStatus("1");
        product.setListingFlag("2");
        product.setDelFlag("0");
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("mall:product:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("mall:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }
    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "wx/{id}")
    public AjaxResult getWxInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductDetailById(id));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("mall:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("mall:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product)
    {
        return toAjax(productService.updateProduct(product));
    }

    @RequiresPermissions("mall:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @GetMapping("/changeListingFlag")
    public AjaxResult changeListingFlag(Long id,String listingFlag)
    {
        return toAjax(productService.changeListingFlag(id,listingFlag));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("mall:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }


    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file)
    {
        return remoteUploadService.upload4Url(file);
    }


    /**
     * 文件上传请求
     */
    @GetMapping("deleteFile")
    public R<Boolean> deleteFile(@RequestParam("fileIds") @Validated String[] fileIds)
    {
        return remoteUploadService.deleteFile(fileIds);
    }

    @PostMapping("uploadOtherPic")
    public AjaxResult uploadOtherPic(@RequestBody UploadPicture uploadPicture){

        return AjaxResult.success(productService.uploadOtherPic(uploadPicture));
    }

    @InnerAuth
    @PostMapping("getRecommendProducts")
    public R<List<Product>> getRecommendProducts(@RequestBody RecommendProductsVO<?> vo){
        return R.ok(recommendProductsStrategy.getRecommendProducts(vo));
    }

}
